#!/bin/bash
cat << eof
############################################
            >>>>SlideR<<<<
       Flash Presentation CreatoR
              Version 0.1
              
         (c) Yudi Rosandi, 2007
###########################################
eof

#
#  words begining wint $ are commands
#  words sorounding by < > are modifiers
#

LANG=C
SLIDERHOME=./
PARSER=$SLIDERHOME/slideparse.awk
FONTDEFAULT=luxisr.swf

die() {
	echo -e $@
	exit 1
}

if [ -f $HOME/.slider_conf ]; then 
	. $HOME/.slider_conf
elif [ -f /etc/slider.conf ]; then
	. $SLIDERHOME/slider_conf
fi

[[ -z $1 ]] && die '
Usage: \n
slider [options] slidetext\n\n	
\tOptions:\n
\t-o <output_file>\n
\t\tsets the name of output file.\n
\t\tby default uses the slidetext name with swf extension.\n
\t-slide <slidename>\n
\t\tonly show the selected slide <slidename>.\n
\t\t<slidename> is defind inside slidetext using "#-->" tag.\n
\t-theme <themename>\n
\t\tuse <themename> as theme\n
\t-listtheme\n
\t\tlist available themes\n
\t-ruler\n
\t\tshow ruler on slides
'

[[ -f $SLIDERHOME/fonts/$FONTDEFAULT ]] || die can not find default font $FONTDEFAULT
[[ -f $PARSER ]] || die can not find parser $PARSER

OUTFILE=
INFILE=
USETHEME=
DOPRINT=
SHOWSLIDE="all"
SHOWRULER=0

while [ ! -z "$1" ]; do
	case "$1" in
	"-o") 
		OUTFILE=$2
		shift 2
		;;
	"-slide")
		SHOWSLIDE=$2
		shift 2
		;;
	"-theme")
		USETHEME=$2
		shift 2
		;;
	"-listtheme")
		echo -e "\nAvailable themes:"
		ls -1 $SLIDERHOME/themes/*|awk '{gsub("'$SLIDERHOME/themes/'","\t");print}'
		die
		;;
	"-print")
		DOPRINT=1;
		shift
		;;
	"-keep")
		KEEPFILES=1;
		shift
		;;
	"-ruler")
		SHOWRULER=1;
		shift
		;;
	*)
		INFILE=$1
		shift
		;;
	esac
done
[ -z "$INFILE" ] && die please give input filename&&exit 1
[ -z "$OUTFILE" ] && OUTFILE=`echo $INFILE|cut -d. -f1`.swf

rm -f slider.log

ls -1 > /tmp/before-$$
awk -f $PARSER -v defaultfont=$FONTDEFAULT -v showslide=$SHOWSLIDE -v outfile=$OUTFILE doprint=$DOPRINT ruleron=$SHOWRULER SliderHome=$SLIDERHOME Theme=$USETHEME $INFILE  > out.sc
ls -1 > /tmp/after-$$


[[ $? -eq 0 ]] || die parsing error

echo parsing finished...
echo now compiling...
swfc out.sc

rm -f slide[0-9]*.swf equ_[0-9].* navigator.swf

[[ -z $KEEPFILES ]] || exit 0

# deletes temporary files
for delf in $(cat /tmp/after-$$); do
	[[ $delf == $INFILE ]] && continue
	[[ $delf == "slider.log" ]] && continue
	[[ $delf == "$(basename $INFILE .txt)" ]] && continue
	grep -q $delf /tmp/before-$$ || rm $delf 2>/dev/null
done

rm -f /tmp/before-$$ /tmp/after-$$
